package xyz.riceball.job.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.riceball.framework.core.enums.ENBool;
import xyz.riceball.job.entity.po.JobLogPO;
import xyz.riceball.job.entity.po.JobTriggerPO;
import xyz.riceball.job.mapper.JobLogMapper;
import xyz.riceball.job.service.JobLogService;

import java.time.LocalDateTime;

/**
 * 定时任务调度日志表 Service实现层
 *
 * @author xiaovcloud
 * @since 2022-06-16 19:02:39
 */
@Service("jobLogService")
public class JobLogServiceImpl extends ServiceImpl<JobLogMapper, JobLogPO> implements JobLogService {

    @Override
    public boolean logSuccess(JobTriggerPO jobTriggerPO, LocalDateTime startTime, LocalDateTime executorTime, String jobMessage) {
        return writeLog(jobTriggerPO, startTime, executorTime, ENBool.TRUE.getValue(), jobMessage, null);
    }

    @Override
    public boolean logError(JobTriggerPO jobTriggerPO, LocalDateTime startTime, LocalDateTime executorTime, String jobMessage, String exceptionInfo) {
        return writeLog(jobTriggerPO, startTime, executorTime, ENBool.FALSE.getValue(), jobMessage, exceptionInfo);
    }

    private boolean writeLog(JobTriggerPO jobTriggerPO, LocalDateTime startTime, LocalDateTime executorTime, String status, String jobMessage, String exceptionInfo) {
        JobLogPO jobLogPO = new JobLogPO();
        jobLogPO.setJobName(jobTriggerPO.getJobName());
        jobLogPO.setExecutorHandler(jobTriggerPO.getExecutorHandler());
        jobLogPO.setExecutorParams(jobTriggerPO.getExecutorParams());
        jobLogPO.setStartTime(startTime);
        jobLogPO.setExecutorTime(executorTime);
        jobLogPO.setStatus(status);
        jobLogPO.setJobMessage(jobMessage);
        jobLogPO.setExceptionInfo(exceptionInfo);

        int insert = getBaseMapper().insert(jobLogPO);
        return insert == 1;
    }
}